package com.eascs.admin.o2o.compensatory.dao;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.compensatory.entity.O2oDCLoanVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;

@Repository
public class O2oDCStagesDao extends BaseO2oDao {

	// 代偿信息分页查询
	public PageResult<Object[]> getDCStagesByPage() {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 100000, 0);
		String querySql = "sql.O2oDCStages.DCStagesByPage";
		sqlPageQuery.setQuerySql(querySql);
		return this.findDataWithPage(sqlPageQuery);
	}

	/**
	 * @Description:
	 *               <p>
	 *               查询代偿数据,传参
	 *               </p>
	 * @author LMX
	 * @date 2017年8月29日 下午3:11:24
	 * @version V1.0
	 * @param vo
	 * @return
	 */
	public PageResult<Object[]> getDCStagesByPage(O2oDCLoanVo vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT c.id, c.loan_billcode, c.loanner_name, ");
		querySql.append(" CONCAT(ol.ltd_code,'-',ol.ltd_name) AS ltd_name, ol.operator_name, ");
		querySql.append(" c.stages_amount, c.stages_term, ea.payments_date,DATE_FORMAT(c.create_time,'%Y-%m-%d') ");
		querySql.append(" FROM o2o_compensatory_back c ");
		querySql.append(" LEFT JOIN o2o_loanapply ol ON c.loan_billcode = ol.bill_code ");
		querySql.append(" LEFT JOIN ( SELECT ea.main_id, ea.payments_date FROM o2o_ea_loan_data_td ea WHERE ea.is_del = 'N' GROUP BY ea.main_id ) ea ON c.id = ea.main_id  ");
		querySql.append(" WHERE /**DATA_PERMISSION(ol.ltd)**/ c.flow_status = 'CLO' AND 1=1 ");
		
		if (!StringUtil.isEmpty(vo.getBillCode())) {
			querySql.append(" AND c.loan_billcode like ?");
			sqlPageQuery.addLikeParam(vo.getBillCode());
		}
		if (!StringUtil.isEmpty(vo.getLoanerName())) {
			querySql.append(" AND c.loanner_name like ?");
			sqlPageQuery.addLikeParam(vo.getLoanerName());
		}
		if (!StringUtil.isEmpty(vo.getStartdate())) {
			querySql.append(" AND c.create_time >= ? ");
			sqlPageQuery.addParam(vo.getStartdate());
		}
		if (!StringUtil.isEmpty(vo.getEnddate())) {
			querySql.append(" AND c.create_time < date_add(?, INTERVAL 1 DAY) ");// 日期加一天,例如今天29号，加一天相当于30号的 00:00，刚好相当于29号的结束24:00
			sqlPageQuery.addParam(vo.getEnddate());
		}
		querySql.append(" ORDER BY c.create_time DESC ");//查询列表按创建时间倒序排列（时间最近的在上面）
		sqlPageQuery.setQuerySql(querySql.toString());
		return this.findDataWithPage(sqlPageQuery);
	}

}
